//	
//  myWebService.m
//  iphoneFirstApp
//
//  Created by ye ye on 11-7-22.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "myWebService.h"
#import <libxml/tree.h>

@implementation myWebService
-(void)setSoapUrl:(NSString *)_soapUrl
{
	soapUrl=_soapUrl;
	
}
-(NSMutableString *) sendMessage:(NSMutableString *)postString		
{
	NSURL *url=	[NSURL URLWithString:soapUrl];
	NSMutableURLRequest *request;
	NSMutableData *postBody=[NSMutableData data];
	request=[[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
	NSString *messageLength=[NSString stringWithFormat:@"%d",[postString length]];
	
	[request addValue:@"application/x-www-form-urlencoded"
   forHTTPHeaderField:@"Content-Type"];
	
	[request addValue:messageLength
   forHTTPHeaderField:@"Content-Length"];
	[request setHTTPMethod:@"POST"];
	[postBody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];
	[request setHTTPBody:postBody];

	 NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	 if (returnData)
	 {
		 NSMutableString *tempString= [[NSMutableString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
		 NSLog(@"%@",tempString);
		 tempString=[tempString stringByReplacingOccurrencesOfString:@"xmlns" withString:@"noNSxml"];
		 NSMutableArray *contents=[NSMutableArray array];
		 NSError *err=nil;
		 DDXMLDocument *myDocu=[[DDXMLDocument alloc] initWithXMLString:tempString options:0 error:&err];
		 if (err)		
		 {
			 @throw [NSException exceptionWithName:@"xmlError" reason:@"xmlString is not a Xml Format" userInfo:nil];
			 return nil;
		 }
		 DDXMLElement *rootElement=[myDocu childAtIndex:0];
		 if (rootElement){
			 return [rootElement stringValue];
		 }else{
			 return nil;
		 }
	
	 }else
	 {
		 return nil;
	 }
	
}

@end
